﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class NewsBussiness : System.Web.UI.Page
    {
        private static NewsBussinessBO newsbussinessbo;
        private static string modeapprove = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                modeapprove = Request.QueryString["approve"];
                string mode = Request.QueryString["Save"];
                //Mode Add                
                if (modeapprove == "true")
                {
                    lblxem_duyet.Text = "Duyệt tin Doanh Nghiệp mới đăng";
                    GridView1.Columns[8].Visible = false;
                    GridView1.Columns[9].Visible = true;
                    GridView1.Columns[2].Visible = false;
                    pn_view.Visible = false;
                    pn_approve.Visible = true;
                    GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessList_ByNotActive_Exprided(false, true, false);
                    GridView1.DataBind();
                    if (mode == "Complete")
                    {
                        pn_result.Visible = true;
                        lblthongbao.Text = "Duyệt tin thành công. Tin đã được đăng.";
                    }
                }
                else
                {
                    if (mode == "Complete")
                    {
                        pn_result.Visible = true;
                        lblthongbao.Text = "Đăng tin thành công.";
                    }
                    GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessBOList();
                    GridView1.DataBind();
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int bussinessid= Convert.ToInt32(GridView1.SelectedRow.Cells[3].Text);
            if (pn_approve.Visible == true)
            {
                Response.Redirect("Edit_AddNewsBussiness.aspx?Mode=" + bussinessid + "&approve=true");
            }
            else
            {
                Response.Redirect("Edit_AddNewsBussiness.aspx?Mode=" + bussinessid);
            }
        }

        protected void btndangtin_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit_AddNewsBussiness.aspx");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    string strScript = "SelectDeSelectHeader(" + ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).ClientID + ");";
                    ((CheckBox)e.Row.Cells[0].FindControl("chkSelect")).Attributes.Add("onclick", strScript);
                }
            }
            catch
            {
                //report error
            }     
        }

        protected void btnxoatin_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("chkSelect");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;

                        int bussinessid = Convert.ToInt32(gvRow.Cells[3].Text);
                        newsbussinessbo= NewsBussinessBO.GetNewsBussinessBO(bussinessid);
                        NewsBussinessBO.DeleteNewsBussinessBO(bussinessid);
                    }
                    catch
                    {
                        pn_result.Visible = true;
                        lblthongbao.Text = "Xóa thất bại.";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn thông tin cần xóa !!!')", true);
                return;
            }
            else
            {
                pn_result.Visible = true;
                lblthongbao.Text = "Xóa thành công !";
                GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessBOList();
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "link")
            {
                string website = ((LinkButton)(((Control)e.CommandSource).Parent.Parent).FindControl("lbtwebsite")).Text;
                Response.Redirect(website);                
            }
        }

        protected void dlkieuxem_SelectedIndexChanged(object sender, EventArgs e)
        {
            pn_result.Visible = false;
            NewsBussinessBOList.KiemTra_TinDN_HetHan();
            if (dlkieuxem.SelectedValue == "tatca")
            {
                GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessBOList();
                GridView1.DataBind();
            }
            else if (dlkieuxem.SelectedValue == "hethan")
            {
                GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessList_ByNotActive_Exprided(true, false, false);
                GridView1.DataBind();
            }
        }

        protected void dlapprove_SelectedIndexChanged(object sender, EventArgs e)
        {
            pn_result.Visible = false;
            NewsBussinessBOList.KiemTra_TinDN_HetHan();
            if (dlapprove.SelectedValue == "moidang")
            {
                GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessList_ByNotActive_Exprided(false, true, false);
                GridView1.DataBind();
            }
            else if (dlapprove.SelectedValue == "moigiahan")
            {
                GridView1.DataSource = NewsBussinessBOList.GetAllNewsBussinessList_ByNotActive_Exprided(false,false,true);
                GridView1.DataBind();
            }
        }

        protected void btnapprove_Click(object sender, EventArgs e)
        {
            bool test = false;
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                CheckBox chkSel = (CheckBox)gvRow.FindControl("cbapprove");
                if (chkSel.Checked == true)
                {
                    try
                    {
                        test = true;
                        int newsbussid = Convert.ToInt32(gvRow.Cells[3].Text);
                        newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(newsbussid);
                        newsbussinessbo.IsActive = true;
                        newsbussinessbo.Save();
                    }
                    catch
                    {
                        pn_result.Visible = true;
                        lblthongbao.Text = "Duyệt tin thất bại.";
                        return;
                    }
                }
            }
            if (test == false)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Chưa chọn tin cần duyệt !!!')", true);
                return;
            }
            else
            {
                dlapprove_SelectedIndexChanged(sender, e);
                pn_result.Visible = true;
                lblthongbao.Text = "Đã duyệt tin(Tin đã được phép đăng).";
            }     
        }

        protected void btnapproveall_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                try
                {
                    int newsbussid = Convert.ToInt32(gvRow.Cells[3].Text);
                    newsbussinessbo = NewsBussinessBO.GetNewsBussinessBO(newsbussid);
                    newsbussinessbo.IsActive = true;
                    newsbussinessbo.Save();
                    dlapprove_SelectedIndexChanged(sender, e);
                    pn_result.Visible = true;
                    lblthongbao.Text = "Đã duyệt tin(Tin đã được phép đăng).";
                }
                catch
                {
                    pn_result.Visible = true;
                    lblthongbao.Text = "Duyệt tin thất bại.";
                }
            } 
        }
    }
}
